﻿<!DOCTYPE html>

<html lang="zh-cn">
<head>
    <meta charset="utf-8" />

    <title>解析</title>
    
    <script src="https://ss.netnr.com/jszip@3.7.1/dist/jszip.min.js"></script>
    <script src="https://ss.netnr.com/file-saver@2.0.5/dist/FileSaver.min.js"></script>
</head>
<body>

    <script>
        var sp = {
            //源
            source: {
                //字
                word: "data/word.json",
                //词
                ci: "data/ci.json",
                //成语
                idiom: "data/idiom.json"
            },
            out: {
                complete: 0,
                word: {
                    rows: 30,
                    keys: [],
                    pagedata: []
                },
                ci: {
                    rows: 600,
                    keys: [],
                    pagedata: []
                },
                idiom: {
                    rows: 150,
                    keys: [],
                    pagedata: []
                }
            },
            zip: null,
            down: function () {
                sp.zip.generateAsync({ type: "blob" }).then(function (content) {
                    saveAs(content, "zidian.zip");
                });
            },
            spword: function () {
                fetch(sp.source.word).then(x => x.json()).then(function (res) {
                    res.forEach(function (item, index) {
                        sp.out.word.keys.push(item.word);
                        var pi = Math.ceil((index + 1) / sp.out.word.rows) - 1;
                        var arr = sp.out.word.pagedata[pi] || [];
                        arr.push(item);
                        sp.out.word.pagedata[pi] = arr;
                    })

                    sp.zip = sp.zip || new JSZip();

                    var text = JSON.stringify(sp.out.word.keys);
                    sp.zip.file("word/00.json", text);

                    sp.out.word.pagedata.forEach(function (pd, pi) {
                        var text = JSON.stringify(pd);
                        sp.zip.file("word/" + pi + ".json", text);
                    });

                    if (++sp.out.complete == 3) {
                        sp.down();
                    }
                })
            },
            spci: function () {
                fetch(sp.source.ci).then(x => x.json()).then(function (res) {
                    res.forEach(function (item, index) {
                        sp.out.ci.keys.push(item.ci);
                        var pi = Math.ceil((index + 1) / sp.out.ci.rows) - 1;
                        var arr = sp.out.ci.pagedata[pi] || [];
                        arr.push(item);
                        sp.out.ci.pagedata[pi] = arr;
                    })

                    sp.zip = sp.zip || new JSZip();

                    var text = JSON.stringify(sp.out.ci.keys);
                    sp.zip.file("ci/00.json", text);

                    sp.out.ci.pagedata.forEach(function (pd, pi) {
                        var text = JSON.stringify(pd);
                        sp.zip.file("ci/" + pi + ".json", text);
                    });

                    if (++sp.out.complete == 3) {
                        sp.down();
                    }
                })
            },
            spidiom: function () {
                fetch(sp.source.idiom).then(x => x.json()).then(function (res) {
                    res.forEach(function (item, index) {
                        sp.out.idiom.keys.push(item.word);
                        var pi = Math.ceil((index + 1) / sp.out.idiom.rows) - 1;
                        var arr = sp.out.idiom.pagedata[pi] || [];
                        arr.push(item);
                        sp.out.idiom.pagedata[pi] = arr;
                    })

                    sp.zip = sp.zip || new JSZip();

                    var text = JSON.stringify(sp.out.idiom.keys);
                    sp.zip.file("idiom/00.json", text);

                    sp.out.idiom.pagedata.forEach(function (pd, pi) {
                        var text = JSON.stringify(pd);
                        sp.zip.file("idiom/" + pi + ".json", text);
                    });

                    if (++sp.out.complete == 3) {
                        sp.down();
                    }
                })
            },
        }

        sp.spword();
        sp.spci();
        sp.spidiom();

    </script>
</body>
</html>